/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.view;

import com.wisnu.scs.controller.DbBarangController;
import com.wisnu.scs.dao.DbBarangDao;
import com.wisnu.scs.dao.DbBarangRekapDao;
import com.wisnu.scs.entity.DbBarang;
import com.wisnu.scs.entity.DbBarangRekap;
import com.wisnu.scs.error.ErrorMessage;
import com.wisnu.scs.event.DbBarangListener;
import com.wisnu.scs.model.DbBarangModel;
import com.wisnu.scs.model.tableModel.DbBarangTableModel;
import com.wisnu.scs.util.HibernateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Wisnu Wardoyo
 */
public class DbBarangView extends javax.swing.JPanel implements DbBarangListener, ListSelectionListener {

    private DbBarangTableModel tableModel;

    private DbBarangModel model;

    private DbBarangController controller;

    private String period = "Semua Data";

    public DbBarangView() {
        tableModel = new DbBarangTableModel();

        model = new DbBarangModel();
        model.setListener(this);

        controller = new DbBarangController();
        controller.setBarangModel(model);

        initComponents();

        tableBarang.getSelectionModel().addListSelectionListener(this);
        tableBarang.setModel(tableModel);
    }

    public JTextField getTxtId() {
        return txtId;
    }

    public JTextField getTxtNamaBarang() {
        return txtNamaBarang;
    }

    public JTextField getTxtStock() {
        return txtStock;
    }

    public JTextField getTxtSearchBox() {
        return txtSearchBox;
    }

    public JTable getTableBarang() {
        return tableBarang;
    }

    public JRadioButton getRadioByDate() {
        return radioByDate;
    }

    public JRadioButton getRadioByMonth() {
        return radioByMonth;
    }

    public JRadioButton getRadioByRange() {
        return radioByRange;
    }

    public JFormattedTextField getTxtFirstDate() {
        return txtFirstDate;
    }

    public JFormattedTextField getTxtLastDate() {
        return txtLastDate;
    }

    public String getPeriod() {
        return period;
    }

    public void setPeriod(String period) {
        this.period = period;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBarang = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        txtSearchBox = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtNamaBarang = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtStock = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        txtFirstDate = new javax.swing.JFormattedTextField();
        txtLastDate = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        radioByDate = new javax.swing.JRadioButton();
        radioByMonth = new javax.swing.JRadioButton();
        radioByRange = new javax.swing.JRadioButton();
        jButton7 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255,255,255,20)
        );

        jPanel1.setBackground(new java.awt.Color(255,255,255,20)
        );
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabel Rekap Barang"));

        tableBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableBarang);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(255,255,255,20)
        );
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Admin Panel"));

        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Operasi");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Nama Barang :");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Stock :");

        txtId.setEditable(false);
        txtId.setEnabled(false);

        jButton2.setText("Tambah");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Ubah");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Reset");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("ID :");

        txtStock.setEditable(false);
        txtStock.setText("0");
        txtStock.setEnabled(false);

        jButton5.setText("Hapus");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Search");

        jButton6.setText("Show All");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        txtFirstDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        txtFirstDate.setEnabled(false);
        txtFirstDate.setValue(new java.util.Date());

        txtLastDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        txtLastDate.setEnabled(false);
        txtLastDate.setValue(new java.util.Date());

        jLabel6.setText("From :");

        jLabel7.setText("To :");

        radioByDate.setText("`By Date");
        radioByDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioByDateActionPerformed(evt);
            }
        });

        radioByMonth.setText("`By Month");
        radioByMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioByMonthActionPerformed(evt);
            }
        });

        radioByRange.setText("`By Range");
        radioByRange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioByRangeActionPerformed(evt);
            }
        });

        jButton7.setText("Print");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(81, 81, 81)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                                .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGap(0, 42, Short.MAX_VALUE))
                            .addComponent(txtSearchBox)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtId)
                                    .addComponent(txtNamaBarang)
                                    .addComponent(txtStock)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtFirstDate, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(radioByRange)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtLastDate, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGap(17, 17, 17)))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioByMonth)
                            .addComponent(radioByDate)
                            .addComponent(jLabel5)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 48, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(10, 10, 10)
                .addComponent(txtSearchBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFirstDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLastDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioByDate)
                    .addComponent(radioByRange))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioByMonth)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(txtNamaBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        controller.insertBarang(this);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        controller.updateBarang(this);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        controller.deleteBarang(this);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        controller.resetForm();
        try {
            loadDatabase();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        controller.searchBarang(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        try {
            loadDatabase();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void radioByDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioByDateActionPerformed
        if (radioByDate.isSelected()) {
            radioByDate.setSelected(true);
            radioByMonth.setSelected(false);
            radioByRange.setSelected(false);

            txtFirstDate.setEnabled(true);
            txtLastDate.setEnabled(false);
        } else {
            radioByDate.setSelected(false);
            radioByMonth.setSelected(false);
            radioByRange.setSelected(false);

            txtFirstDate.setEnabled(false);
            txtLastDate.setEnabled(false);
        }
    }//GEN-LAST:event_radioByDateActionPerformed

    private void radioByMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioByMonthActionPerformed
        if (radioByMonth.isSelected()) {
            radioByDate.setSelected(false);
            radioByMonth.setSelected(true);
            radioByRange.setSelected(false);

            txtFirstDate.setEnabled(true);
            txtLastDate.setEnabled(false);
        } else {
            radioByDate.setSelected(false);
            radioByMonth.setSelected(false);
            radioByRange.setSelected(false);

            txtFirstDate.setEnabled(false);
            txtLastDate.setEnabled(false);
        }        // TODO add your handling code here:
    }//GEN-LAST:event_radioByMonthActionPerformed

    private void radioByRangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioByRangeActionPerformed
        if (radioByRange.isSelected()) {
            radioByDate.setSelected(false);
            radioByMonth.setSelected(false);
            radioByRange.setSelected(true);

            txtFirstDate.setEnabled(true);
            txtLastDate.setEnabled(true);
        } else {
            radioByDate.setSelected(false);
            radioByMonth.setSelected(false);
            radioByRange.setSelected(false);

            txtFirstDate.setEnabled(false);
            txtLastDate.setEnabled(false);
        }        // TODO add your handling code here:
    }//GEN-LAST:event_radioByRangeActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        controller.print(this);
    }//GEN-LAST:event_jButton7ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton radioByDate;
    private javax.swing.JRadioButton radioByMonth;
    private javax.swing.JRadioButton radioByRange;
    private javax.swing.JTable tableBarang;
    private javax.swing.JFormattedTextField txtFirstDate;
    private javax.swing.JTextField txtId;
    private javax.swing.JFormattedTextField txtLastDate;
    private javax.swing.JTextField txtNamaBarang;
    private javax.swing.JTextField txtSearchBox;
    private javax.swing.JTextField txtStock;
    // End of variables declaration//GEN-END:variables

    @Override
    public void onReset(DbBarangModel barangModel) {
        txtId.setText(barangModel.getId() + "");
        txtNamaBarang.setText(barangModel.getNamaBarang());
        txtStock.setText(barangModel.getStock() + "");
        txtSearchBox.setText(barangModel.getSearchText());
    }

    @Override
    public void onInsert(DbBarang barang) {
        try {
            loadDatabase();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void onUpdate(DbBarang barang) {
        try {
            loadDatabase();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void onDelete(DbBarang barang) {
        try {
            loadDatabase();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent lse) {
        try {
            DbBarang barang = tableModel.get(tableBarang.getSelectedRow());
            txtId.setText(barang.getIdBarang() + "");
            txtNamaBarang.setText(barang.getNamaBarang());
            txtStock.setText(barang.getStock() + "");
        } catch (IndexOutOfBoundsException exception) {

        }
    }

    public void loadDatabase() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();
        DbBarangRekapDao barangRekapDao = HibernateUtil.getBarangRekapDao();
        tableModel = new DbBarangTableModel();
        tableModel.setListDbBarang(barangDao.selectAllBarang());
        tableModel.setListDbBarangRekap(barangRekapDao.selectAll(barangDao.selectAllBarang()));
        tableBarang.setModel(tableModel);
        repaint();
    }

    @Override
    public void onSearch(List<DbBarang> barang) {
        try {
            DbBarangRekapDao barangRekapDao = HibernateUtil.getBarangRekapDao();
            tableModel = new DbBarangTableModel();
            tableModel.setListDbBarang(barang);
            tableModel.setListDbBarangRekap(barangRekapDao.selectAll(barang));
            tableBarang.setModel(tableModel);
            repaint();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void onSearchByDate(List<DbBarang> barang, Date date) {
        try {
            DbBarangRekapDao barangRekapDao = HibernateUtil.getBarangRekapDao();
            tableModel = new DbBarangTableModel();
            tableModel.setListDbBarang(barang);
            tableModel.setListDbBarangRekap(barangRekapDao.selectByDate(barang, date));
            tableBarang.setModel(tableModel);
            repaint();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void onSearchByDateRange(List<DbBarang> barang, List<Date> dateList) {
        try {
            DbBarangRekapDao barangRekapDao = HibernateUtil.getBarangRekapDao();
            tableModel = new DbBarangTableModel();
            tableModel.setListDbBarang(barang);
            tableModel.setListDbBarangRekap(barangRekapDao.selectByDateRange(barang, dateList));
            tableBarang.setModel(tableModel);
            repaint();
        } catch (ErrorMessage ex) {
            Logger.getLogger(DbBarangView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void onSearch(List<DbBarang> barang, List<DbBarangRekap> barangRekap) {
        tableModel = new DbBarangTableModel();
        tableModel.setListDbBarang(barang);
        tableModel.setListDbBarangRekap(barangRekap);
        tableBarang.setModel(tableModel);
        repaint();
    }

}
